﻿using System;
using System.Collections.Generic;
using BO.Infrastructure.StaticData;
using BO.Infrastructure.Utils;
using BO.Video;

namespace GUI.Controls.Sport
{
    public partial class BoxVideo : System.Web.UI.UserControl
    {
        public int ZoneId { get; set; }
        public string ViewMoreUrl { get; set; }
        public string BoxTitle { get; set; }

        private const string TOPFormat = @"{3}<p><a rel=""{4}"" title=""{0}"" href=""{2}"">{1}</a></p>";
        private const string LiFormat = @"<li><span class=""spritethethao fl""></span><a title=""{0}"" rel=""{3}"" href=""{2}"">{1}</a></li>";
        private const int ImageWidth = 264;
        private const int ImageHeight = 180;

        public string VideoData { get; private set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            var dt = VideoBO.GetNewestVideo(top: 3, zoneId: ZoneId);
            Visible = DataBind(dt);
            if (Visible)
            {
                if (string.IsNullOrEmpty(ViewMoreUrl) || string.IsNullOrEmpty(BoxTitle))
                {
                    var objCat = VideoBO.GetVideoZoneDetail(ZoneId);
                    if (objCat != null)
                    {
                        ViewMoreUrl =  ViewMoreUrl ?? objCat.Url;
                        BoxTitle = BoxTitle ?? objCat.Name;
                    }
                }
                ltrTitle.Text = string.Format("<a title=\"{1}\" href=\"{0}\">{1}</a>", ViewMoreUrl, BoxTitle);
            }
        }

        private bool DataBind(System.Data.DataTable dt)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                VideoData = PresentationUtils.ObjectToJson(dt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var r = dt.Rows[i];
                    if (i < 1)//1 tin top
                    {
                        //var avatar = PresentationUtils.Photo.GetThumbImage_Zoom(
                        //    r[SqlConstants.Video.Avatar].ToString(), ImageWidth, ImageHeight);

                        var avatar = PresentationUtils.Photo.BuildVideoAvatar(r, ImageWidth, ImageHeight, new[] { new KeyValuePair<string, string>("rel", r[SqlConstants.Video.Id].ToString()) });

                        ltrTop.Text += string.Format(TOPFormat,
                            r[SqlConstants.Video.EncodedName],
                            r[SqlConstants.Video.Name],
                            r[SqlConstants.Video.Url],
                            avatar,
                            r[SqlConstants.Video.Id]
                            );
                    }
                    else//2 tin dưới
                    {

                        this.ltrList.Text += string.Format(LiFormat,
                            r[SqlConstants.Video.EncodedName],
                            r[SqlConstants.Video.Name],
                            r[SqlConstants.Video.Url],
                            r[SqlConstants.Video.Id]
                            );
                    }
                }
                return true;
            }
            return false;
        }
    }
}